<template>
  <static-content-wrapper :designer="designer" :settings="settings" :field="field" :design-state="designState"
    :hidden="field.options.hidden" :parent-widget="parentWidget" :parent-list="parentList"
    :index-of-parent-list="indexOfParentList" :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex" :sub-form-row-id="subFormRowId" :allow-select="allowSelect">
    <el-image ref="imgRef" class="field-editor picture-field-editor" :src="pictrueUrl" fit="cover"
      :style="pictureStyle" />
  </static-content-wrapper>
</template>

<script>
import emitter from '@/mixins/emitter'
import fieldMixin from '@/mixins/fieldMixin'
import * as EVENTS from '@/designer/event'

export default {
  name: 'PictureWidget',
  componentName: 'FieldWidget',
  // 必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    settings: Object,
    scale: {
      type: Number,
      default: 1
    },
    allowSelect: {
      type: Boolean,
      default: true
    },

    designState: {
      type: Boolean,
      default: false
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: ''
    }
  },
  data() {
    return {
      pictureStyle: {
        width: 100 * this.scale + 'px',
        height: 100 * this.scale + 'px'
      },
      pictrueUrl: ''
    }
  },
  computed: {},
  watch: {
    'field.options': {
      deep: true,
      handler(val) {
        if (val) {
          if (val.pngId) {
            this.getImgStart()
          } else {
            this.pictrueUrl = ''
            this.pictureStyle = {
              width: 100 * this.scale + 'px',
              height: 100 * this.scale + 'px'
            }
          }
        }
      }
    }
  },
  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */
    this.registerToRefList()
    this.initEventHandler()

    this.handleOnCreated()
  },
  mounted() {
    this.handleOnMounted()
    this.$emitter.on(EVENTS.GET_IMAGE_RESOURCE_RESULT, this.getImageResourceResult)
    console.log(this.field.options)
    if (this.field.options.pngId) {
      this.getImgStart()
    }
  },
  beforeUnmount() {
    this.unregisterFromRefList()
  },
  methods: {
    getImgStart() {
      let params = {
        name: this.field.options.pngId,
        id: this.field.options.id,
        extend: null
      }
      this.$emitter.emit(EVENTS.GET_IMAGE_RESOURCE, params)
    },

    getImageResourceResult(data) {
      if (data.id != this.field.options.id) {
        return
      }

      console.log(data)
      if (data && data.name === this.field.options.pngId) {
        if (data.result) {
          this.pictrueUrl = data.result.url
          this.pictureStyle = {}
        } else {
          this.pictrueUrl = ''
          this.pictureStyle = {
            width: 100 * this.scale + 'px',
            height: 100 * this.scale + 'px'
          }
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.picture-field-editor {
  :deep(.el-image__inner) {
    width: fit-content;
    height: fit-content;
  }
}
</style>
